package workers;

import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;

import model.Software;
import workerUIs.SpecificationStepFrame;
import workerUIs.StepFrame;
import workflow.IEventListener;
import workflow.WorkflowEvent;
import workflow.WorkflowEvent.EventType;
import workflow.step.SpecificationStep;

/**
 * SpecificationStepWorker class.
 * 
 * @author Mark Czotter
 * @author Miklos Szennai
 * 
 */
public class SpecificationStepWorker extends AbstractStepSwingWorker {

	public SpecificationStepWorker(SpecificationStepFrame frame, BlockingQueue<Software> outputQueue, StepFrame nextFrame, final IEventListener listener) {
		super(frame, null, outputQueue, nextFrame, listener);
	}

	@Override
	protected Software doInBackground() throws Exception {
		try {
			this.software = doStep(null);
			if (listener != null) listener.onEvent(WorkflowEvent.createEvent(this.software.getId(), getTaskName(), EventType.START));
			Thread.sleep(delay);
			this.outputQueue.put(this.software);
			if (listener != null) listener.onEvent(WorkflowEvent.createEvent(this.software.getId(), getTaskName(), EventType.END));
			return this.software;
		} catch (Exception e) {
			e.printStackTrace();
			logger.log(Level.SEVERE, "Exception when executing step: " + getClass().getName() + ": " + e.getMessage());
		}
		return null;
	}
	
	@Override
	protected String getTaskName() {
		return "spec";
	}
	
	@Override
	protected Software doStep(Software software) {
		SpecificationStep step = new SpecificationStep();
		return step.doStep(null);
	}

}
